Skip to content

FilterHeader

Base classes

Name Children Inherits
HeaderView
prettyqt.widgets.headerview
Header row or header column for item views.

â‹” Inheritance diagram

graph TD
  1525557315["custom_widgets.FilterHeader"]
  2346798028["widgets.HeaderView"]
  1948211140["widgets.HeaderViewMixin"]
  3836649406["widgets.AbstractItemViewMixin"]
  1259044718["widgets.AbstractScrollAreaMixin"]
  1689631995["widgets.FrameMixin"]
  829357528["widgets.WidgetMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  625385617["gui.PaintDeviceMixin"]
  2480657522["QtWidgets.QHeaderView"]
  3038536384["QtWidgets.QAbstractItemView"]
  1459025998["QtWidgets.QAbstractScrollArea"]
  3822057598["QtWidgets.QFrame"]
  3620195106["QtWidgets.QWidget"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  673956333["QtGui.QPaintDevice"]
  2346798028 --> 1525557315
  1948211140 --> 2346798028
  3836649406 --> 1948211140
  1259044718 --> 3836649406
  1689631995 --> 1259044718
  829357528 --> 1689631995
  3227531497 --> 829357528
  2751680898 --> 3227531497
  625385617 --> 829357528
  2751680898 --> 625385617
  2751680898 --> 829357528
  2751680898 --> 1689631995
  2751680898 --> 1259044718
  2751680898 --> 3836649406
  2751680898 --> 1948211140
  2480657522 --> 2346798028
  3038536384 --> 2480657522
  1459025998 --> 3038536384
  3822057598 --> 1459025998
  3620195106 --> 3822057598
  3984060476 --> 3620195106
  37984305 --> 3984060476
  2751680898 --> 37984305
  673956333 --> 3620195106
  37984305 --> 673956333

🛈 DocStrings

Bases: HeaderView

A HeaderView subclass which includes widgets with filter possibilities.

When setting the header view on an ItemView, a proxy model will be created which is linked to the filter widgets. The correct filter widget is automatically inferred from the content of the columns.

So basically everything that needs to be done is the following:

model = MyModel()
widget = widgets.TableView()
widget.set_model(model)
widget.h_header = custom_widgets.FilterHeader()  # same as setHorizontalHeader()

and you will get filter capabilities for your table.

!!! note Since the FilterHeader will infer the column content type based on the first few rows, it will only work correctly for tables with homogenous data.

![Image title](../../images/filterheader.png)
FilterHeader widget
Source code in prettyqt/custom_widgets/itemviews/filterheader.py
class FilterHeader(widgets.HeaderView):
    """A HeaderView subclass which includes widgets with filter possibilities.

    When setting the header view on an ItemView, a proxy model will be created which is
    linked to the filter widgets.
    The correct filter widget is automatically inferred from the content of the columns.

    So basically everything that needs to be done is the following:

    ```py
    model = MyModel()
    widget = widgets.TableView()
    widget.set_model(model)
    widget.h_header = custom_widgets.FilterHeader()  # same as setHorizontalHeader()
    ```

    and you will get filter capabilities for your table.

    !!! note
        Since the FilterHeader will infer the column content type based on the first few
        rows, it will only work correctly for tables with homogenous data.

    <figure markdown>
      ![Image title](../../images/filterheader.png)
      <figcaption>FilterHeader widget</figcaption>
    </figure>

    """

    def __init__(self, parent: widgets.TableView | widgets.TreeView):
        self._editors_visible = False
        self._editors = []
        self._proxy: MultiColumnFilterProxyModel = parent.proxifier.get_proxy(  # type: ignore
            "multi_column_filter",
            recursive_filtering_enabled=True,
        )
        self._padding = 6
        super().__init__(constants.HORIZONTAL, parent)
        self.setStretchLastSection(True)
        # self.setResizeMode(QHeaderView.Stretch)
        self.setDefaultAlignment(constants.ALIGN_CENTER_LEFT)
        # self.setSortIndicatorShown(False)
        self.sectionResized.connect(self._adjust_positions)
        parent.h_scrollbar.valueChanged.connect(self._adjust_positions)
        parent.model_changed.connect(self._update_filter_boxes)
        self._update_filter_boxes()
        self.updateGeometries()

    @classmethod
    def setup_example(cls):
        w = widgets.TableView()
        return cls(parent=w)

    def are_editors_visible(self) -> bool:
        return self._editors_visible

    def set_editors_visible(self, visible: bool):
        self._editors_visible = visible
        for editor in self._editors:
            editor.setVisible(visible)
        self.updateGeometries()

    def _update_filter_boxes(self):
        # TODO: deal with column changes by connecting to Model signals.
        # That way we wouldnt have to update all editors on change.
        while self._editors:
            editor = self._editors.pop()
            editor.deleteLater()
        self.create_editors()
        self._adjust_positions()

    def create_editors(self):
        # using parent model here bc we cant guarantee that we are already set to view.
        parent = self.parent()
        if not isinstance(parent, widgets.QAbstractItemView):
            return
        self._proxy.clear_filters()
        raw_model = parent.model()
        if raw_model is None:
            return
        model = cast("core.AbstractItemModel", raw_model)
        for i in range(model.columnCount()):
            typ = model.get_column_type(i)
            if typ is bool:

                def set_filter(val, i=i):
                    self._proxy.set_filter_value(i, val, constants.CHECKSTATE_ROLE)

                name = f"filter_combo_{i}"
                widget = widgets.ComboBox(margin=0, object_name=name, parent=self)
                widget.add_items(BOOL_ITEMS)
                widget.value_changed.connect(set_filter)
            elif typ in [int, float]:

                def set_filter(val, i=i):
                    self._proxy.set_filter_value(i, val)

                name = f"filter_numwidget_{i}"
                widget = custom_widgets.NumFilterWidget(
                    margin=0, object_name=name, parent=self
                )
                widget.filter_changed.connect(set_filter)
                title = model.headerData(i, constants.HORIZONTAL, constants.DISPLAY_ROLE)
                widget.lineedit.setPlaceholderText(f"Filter {title}...")
            elif typ is str:

                def set_filter(val, i=i):
                    self._proxy.set_filter_value(i, val)

                name = f"filter_lineedit_{i}"
                widget = widgets.LineEdit(margin=0, object_name=name, parent=self)
                widget.value_changed.connect(set_filter)
                title = model.headerData(i, constants.HORIZONTAL, constants.DISPLAY_ROLE)
                widget.setPlaceholderText(f"Filter {title}...")
            else:
                widget = widgets.Widget(parent=self)
            widget.setVisible(self._editors_visible)
            self._editors.append(widget)

    def sizeHint(self) -> core.QSize:
        size = super().sizeHint()
        if self._editors:
            height = self._editors[0].sizeHint().height()
            size.setHeight(size.height() + height + self._padding)
        return size

    def updateGeometries(self):
        if self._editors:
            height = self._editors[0].sizeHint().height()
            self.setViewportMargins(0, 0, 0, height + self._padding)
        else:
            self.setViewportMargins(0, 0, 0, 0)
        super().updateGeometries()
        self._adjust_positions()

    def _adjust_positions(self):
        for index, editor in enumerate(self._editors):
            height = editor.sizeHint().height()
            compensate_y = 0
            compensate_x = 0
            match editor:
                case widgets.QComboBox():
                    compensate_y = +2
                case widgets.QPushButton():
                    compensate_y = -1
                case widgets.QCheckBox():
                    compensate_y = 4
                    compensate_x = 4
                case widgets.QWidget():
                    compensate_y = -1
            editor.move(
                self.sectionPosition(index) - self.offset() + 1 + compensate_x,
                height + (self._padding // 2) + compensate_y,
            )
            editor.resize(self.sectionSize(index), height)

    def set_filter_case_sensitive(self, value: bool):
        self._proxy.set_filter_case_sensitive(value)

    def clear_filters(self):
        for editor in self._editors:
            editor.clear()

    editors_visible = core.Property(
        bool,
        are_editors_visible,
        set_editors_visible,
        doc="Whether the filter widgets are visible",
    )

:fa-brands-github: Show source on GitHub